<template>
	<view class="dynamic-follow pt-md" :style="{background:pageColor}">
		<view @tap.stop="goDetail(index)" class="list-item flex-center fill-base ml-md mr-md pd-lg radius-16 box-shadow"
			:class="[{'mt-md':index!=0}]" v-for="(item,index) in list.data" :key="index">
			<image mode="aspectFill" class="avatar radius" :src="item.work_img"></image>
			<view class="flex-1 ml-md">
				<view class="flex-between">
					<view class="f-title c-black text-bold ellipsis" style="max-width: 150rpx;">
						{{item.coach_name || '-'}}
					</view>
					<view class="flex-y-baseline f-desc c-black"> <i class="iconfont iconjuli"
							:style="{color:primaryColor}"></i>
						{{item.distance}}
					</view>
				</view>
				<view class="flex-y-baseline">
					<view class="text flex-y-center f-desc">已接单<view class="ml-sm" :style="{color:primaryColor}">
							{{item.order_num}}
						</view>
					</view>
					<view class="flex-y-center f-caption c-caption ml-md"> 粉丝数 <view class="c-title ml-sm">
							{{item.fans_num}}
						</view>
					</view>
				</view>
			</view>
		</view>
		<load-more :noMore="list.current_page>=list.last_page&&list.data.length>0" :loading="loading" v-if="loading">
		</load-more>
		<abnor type="USER" :isCenter="true" v-if="!loading&&list.data.length<=0&&list.current_page==1"></abnor>
		<view class="space-footer"></view>
	</view>
</template>

<script>
	import {
		mapState,
		mapActions,
		mapMutations
	} from "vuex"
	export default {
		data() {
			return {
				loading: true,
				param: {
					page: 1
				},
				list: {
					data: []
				}
			}
		},
		computed: mapState({}),
		onLoad() {
			this.$util.setNavigationBarColor({
				bg: this.primaryColor
			})
			this.initIndex()
		},
		onPullDownRefresh() {
			// #ifndef APP-PLUS
			uni.showNavigationBarLoading()
			// #endif
			this.initRefresh();
			uni.stopPullDownRefresh()
		},
		onReachBottom() {
			if (this.list.current_page >= this.list.last_page || this.loading) return;
			this.param.page = this.param.page + 1;
			this.loading = true;
			this.getList();
		},
		watch: {
			// #ifndef H5
			changeOnAddr(newval, oldval) {
				let {
					noloca,
				} = this.noChangeLoca
				if (newval && noloca) {
					this.initUtilLocaData()
				}
			},
			noChangeLoca(newval, oldval) {
				setTimeout(() => {
					let {
						lat,
						lng,
						unix = 0
					} = this.changeOnAddr
					let {
						noloca
					} = this.noChangeLoca
					let cur_unix = this.$util.DateToUnix(this.$util.formatTime(new Date(), 'YY-M-D h:m:s'))
					if (noloca && ((!lat && !lng) || !unix || (unix && (cur_unix - unix >= 1)))) {
						this.getUtilLocation()
					}
				}, 800)
			}
			// #endif
		},
		methods: {
			...mapMutations(['updateUserItem', 'updateMapItem']),
			async initIndex(refresh = false) {
				await this.isNotLoca()
				// #ifdef H5
				if (!refresh) {
					this.$jweixin.hideOptionMenu()
				}
				// #endif
			},
			initRefresh() {
				this.param.page = 1
				this.initIndex(true)
			},
			async isNotLoca() {
				let {
					location,
					locaRefuse
				} = this

				let {
					status: coach_status = 0,
					coach_position = 0
				} = this.userCoachStatus
				
				
				// #ifdef H5
				if (coach_position && coach_status == 2 && (location.lat && location.address != '暂未获取到位置信息')) {
					this.updateUserItem({
						key: 'isChangeCoachLoca',
						val: true
					})
				}
				// #endif
				

				// #ifdef APP-PLUS
				if (!locaRefuse && (!location.lat || (location.lat && location.address == '暂未获取到位置信息'))) {
					// #endif
					// #ifndef APP-PLUS
					if (!location.lat || (location.lat && location.address == '暂未获取到位置信息')) {
						// #endif

						// #ifndef H5
						if (coach_position && coach_status == 2) {
							let {
								lat: change_lat = 0,
								lng: change_lng = 0,
								unix = 0
							} = this.changeOnAddr
							let cur_unix = this.$util.DateToUnix(this.$util.formatTime(new Date(), 'YY-M-D h:m:s'))
							let noloca = change_lat && change_lng && (unix && (cur_unix - unix < 3)) ? false : true
							if (!noloca) {
								let loca = Object.assign({}, this.location, {
									lat: change_lat,
									lng: change_lng,
									is_util_loca: 1
								})
								this.updateUserItem({
									key: 'location',
									val: loca
								})
							}
							this.updateUserItem({
								key: 'noChangeLoca',
								val: {
									noloca
								}
							})
							if (this.noChangeLoca.noloca) return
							this.initUtilLocaData()
						} else {
							this.getUtilLocation()
						}
						this.updateMapItem({
							key: 'pageActive',
							val: false
						})
						return
						// #endif

						// #ifdef H5
						await this.$util.getUtilLocation()
						this.updateMapItem({
							key: 'pageActive',
							val: false
						}) 
						// #endif
					}
					this.initUtilLocaData()
				},
				async getUtilLocation() {
						await this.$util.getUtilLocation()
						this.initUtilLocaData()
					},
					async initUtilLocaData() {
							// #ifndef H5
							this.updateUserItem({
								key: 'noChangeLoca',
								val: {
									noloca: false
								}
							})
							// #endif 
							await this.getList()
							let {
								lat = 0, lng = 0, is_util_loca = 0, isIp = true
							} = this.location
							if (lat && lng && is_util_loca) {
								this.$util.getMapInfo(isIp)
							}
						},
						async getList() {
								let {
									list: oldList
								} = this

								let {
									lat = 0, lng = 0
								} = this.location
								let param = Object.assign({}, this.param, {
									lat,
									lng
								})

								let newList = await this.$api.dynamic.followCoachList(param)
								if (this.param.page == 1) {
									this.list = newList
								} else {
									newList.data = oldList.data.concat(newList.data)
									this.list = newList
								}
								this.loading = false
								this.$util.hideAll()
							},
							goDetail(index) {
								let {
									coach_id
								} = this.list.data[index]
								this.$util.goUrl({
									// url: `/user/pages/technician-info?id=${coach_id}`
									url: `/userA/pages/worker-data?id=${coach_id}`
									
								})
							}
			}
		}
</script>

<style lang="scss">
	.dynamic-follow {
		.list-item {
			.avatar {
				width: 124rpx;
				height: 124rpx;
			}

			.text {
				color: #4D4D4D;
				margin-top: 6rpx;
			}
		}
	}
</style>